package com.hyk.commonLib.common.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Outline;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewOutlineProvider;

import androidx.annotation.ColorInt;
import androidx.annotation.RequiresApi;

import com.hyk.commonLib.R;
import com.hyk.commonLib.common.utils.ResUtils;

/**
 * Created by 贺玉琨 on 2019/3/14
 */

public class RoundCornerEditText extends CustomerSizeDrawableEditText {

    @ColorInt
    int backColor;
    @ColorInt
    int borderColor;
    float cornerRadiusLT;
    float cornerRadiusRT;
    float cornerRadiusLB;
    float cornerRadiusRB;
    int borderWidth;

    public RoundCornerEditText(Context context) {
        this(context, null);
    }

    public RoundCornerEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundCornerEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (attrs != null) {
            initBackground(attrs);
        }
        initView();
    }

    private void initView() {
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    private void initBackground(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundCornerEditText);
        backColor = a.getColor(R.styleable.RoundCornerEditText_backColor, ResUtils.getColor(R.color.default_edit_back_color));
        float cornerRadius = a.getDimension(R.styleable.RoundCornerEditText_cornerRadius, 5);
        cornerRadiusLT = a.getDimension(R.styleable.RoundCornerEditText_cornerRadiusLT, cornerRadius);
        cornerRadiusRT = a.getDimension(R.styleable.RoundCornerEditText_cornerRadiusRT, cornerRadius);
        cornerRadiusLB = a.getDimension(R.styleable.RoundCornerEditText_cornerRadiusLB, cornerRadius);
        cornerRadiusRB = a.getDimension(R.styleable.RoundCornerEditText_cornerRadiusRB, cornerRadius);
        borderColor = a.getColor(R.styleable.RoundCornerEditText_borderColor, Color.WHITE);
        borderWidth = (int) a.getDimension(R.styleable.RoundCornerEditText_borderWidth, 0);
        a.recycle();
        refreshBackground();
    }

    public RoundCornerEditText setBackColor(@ColorInt int backColor) {
        this.backColor = backColor;
        return this;
    }

    public RoundCornerEditText setBorderColor(@ColorInt int borderColor) {
        this.borderColor = borderColor;
        return this;
    }

    public RoundCornerEditText setCornerRadius(float cornerRadius) {
        this.cornerRadiusLT = cornerRadius;
        this.cornerRadiusRT = cornerRadius;
        this.cornerRadiusLB = cornerRadius;
        this.cornerRadiusRB = cornerRadius;
        return this;
    }

    public RoundCornerEditText setBorderWidth(int borderWidth) {
        this.borderWidth = borderWidth;
        return this;
    }

    public void refreshBackground() {
        GradientDrawable normalDrawable = new GradientDrawable();
        normalDrawable.setColor(backColor);
        normalDrawable.setStroke(borderWidth, borderColor);
//        top-left, top-right, bottom-right, bottom-left.
        normalDrawable.setCornerRadii(new float[]{
                cornerRadiusLT, cornerRadiusLT,
                cornerRadiusRT, cornerRadiusRT,
                cornerRadiusRB, cornerRadiusRB,
                cornerRadiusLB, cornerRadiusLB,
        });
        setBackground(normalDrawable);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setTranslationZ(0);
            setElevation(0);
            //获取outLine，我们需要使用ViewoutLineProvider
            ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void getOutline(View view, Outline outline) {
//                    //修改outLine的形状，这里是设置分别设置左上右下，以及Radius
//                    outline.setRoundRect(0,0,view.getWidth(),view.getHeight(),30);
                }
            };
            //将需要控件重写设置形状
            setOutlineProvider(viewOutlineProvider);
        }
    }
}
